
using System;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Linq;
using System.IO;
using System.Net;

namespace Hanselminutes
{
	public class Show
	{
		static string _podcastsPath = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
		
		public string Url { get; private set; }
		
		public int ShowNumber { get; set; }
		public string Title { get; set; }
		public DateTime PublishedDate { get; set; }
		public string Description { get; set; }
		public string CommentsUrl { get; set; }
		
		public string OfflineAudioPath { get; private set; }
		public string PartialAudioPath { get; private set; }

		public Show (string url)
		{
			Url = url;
			
			var fileName = Path.GetFileNameWithoutExtension (Url);
			var extension = Path.GetExtension(Url);
			
			Console.WriteLine ("Extension = " + url);
			if(extension == ".mp4"){
				OfflineAudioPath = url;
				PartialAudioPath = Path.Combine (_podcastsPath, fileName + "_partial" + extension);
			}
			else {
				OfflineAudioPath = Path.Combine (_podcastsPath, fileName + extension);
				PartialAudioPath = Path.Combine (_podcastsPath, fileName + "_partial" + extension);
			}
		}		
		
		/// <summary>
		/// Checks if a Offline Mp3 is available.
		/// </summary>
		public bool OfflineAudioAvailable {
			get {
				Console.WriteLine ("File exist = " + OfflineAudioPath);
				return File.Exists(OfflineAudioPath);
			}
		}
		
	}
	
	public class Comment {
		public string HtmlText { get; private set; }
		public string Author { get; private set; }
		
		public Comment(string text, string author) {
			HtmlText = text;
			Author = author;
		}
		
		public static Comment[] ParseRss(string rss) {
			var doc = XDocument.Parse(rss);
			var items = doc.Descendants("item");
			var cs =	from i in items
					select new Comment(
						i.Element("description").Value,
						i.Element("title").Value
				                   );
			return cs.ToArray();
		}
	}
}
